package com.bsoft.gol.hcb.dao.prescriptionCirculation;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.annotation.RpcService;
import ctd.util.converter.ConversionUtils;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import pcn.bean.consultation.ExpreeOrderDetailResponseBean;
import pcn.prescriptionCirculation.OdsOrderPrescriptionDrug;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description: 订单处方记录表
 * @author: chenJij@bsoft.com.cn
 * @date: 2021/8/30
 */
public abstract class OdsOrderPrescriptionDrugDAO extends HibernateSupportDelegateDAO<OdsOrderPrescriptionDrug> {
    public OdsOrderPrescriptionDrugDAO() {
        this.setKeyField("orderPrescriptionDrugId");
    }

    @DAOMethod(sql = "from OdsOrderPrescriptionDrug where orderPrescriptionInfoId =:orderPrescriptionInfoId")
    public abstract List<OdsOrderPrescriptionDrug> findByOrderPrescriptionInfoId(@DAOParam("orderPrescriptionInfoId")String orderPrescriptionInfoId);


    @DAOMethod(sql = " select a from OdsOrderPrescriptionDrug a,OdsOrderPrescriptionInfo b  where a.orderPrescriptionInfoId = b.orderPrescriptionInfoId and  b.orderId=:orderId")
    public abstract List<OdsOrderPrescriptionDrug> findByOrderId(@DAOParam("orderId")String orderId);

    @RpcService
    public List<ExpreeOrderDetailResponseBean.Prescription> findOdsPrescriptionInfos(final String orderId){
        HibernateStatelessResultAction<List<ExpreeOrderDetailResponseBean.Prescription>> action = new AbstractHibernateStatelessResultAction<List<ExpreeOrderDetailResponseBean.Prescription>>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuilder sql = new StringBuilder(
                        "select " +
                        "       opi.prescription_number as prescriptionNumber, " +
                        "       opi.prescription_type as type, " +
                        "       opi.boil_sign as boilSign, " +
                        "       opi.number_of_packets as numberOfPackets, " +
                        "       opd.drug_name as drugName, " +
                        "       opd.dose_unit as company, " +
                        "       opd.quantity as number, " +
                        "       opd.org_factory_name as placeOfOrigin, " +
                        "       opd.price as unitPrice," +
                        "       opi.prescription_type as drugType  " +
                        "       from ods_order_prescription_info opi " +
                        "       left join ods_order_prescription_drug opd on opd.order_prescription_info_id = opi.order_prescription_info_id " +
                        "       where opi.order_id=:orderId");
                Query query = ss.createSQLQuery(sql.toString());
                query.setParameter("orderId",orderId);
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> dbList = query.list();
                if (dbList.size() == 0) {
                    return;
                }
                List<ExpreeOrderDetailResponseBean.Prescription> prescriptions = new ArrayList<>();
                for (Map map : dbList) {
                    prescriptions.add(ConversionUtils.convert(map, ExpreeOrderDetailResponseBean.Prescription.class));
                }
                setResult(prescriptions);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}
